// import $ from './lib/jquery';
import Hooks from './common/hooks';
import tools from './common/tools';
import tips from './common/tips';
import Taber from './common/tab';
import Pinyin from './common/pinyin';
import mainui from './ui';
import Sheets from './sheets';

class Marker {
    constructor(host){
        this.host=host;
        this.hooks=new Hooks();
        this.file=null;
        this.init();
    }
    init(){
        let that=this;
        this.ui=new mainui(this.host,this);
        this.ui.addHook('changeCellProps',function(o){
            var e=o.params.event,v=o.params.data;
            that.sheets.updateSelectedCells(e,v);
        });
        this.ui.addHook('menucallback',function(o){
            console.log('menucallback',o);
        });
        this.ui.addHook('opensheet',function(o){
            let file=o.params.data,
                type=o.params.type||0;
            if(type===2){
                if(file.type==='items'){
                    file.sheets[0].colHeaders=that.parseHeader('col',file.sheets[0].colheader);
                    file.sheets[0].rowHeaders=that.parseHeader('row',file.sheets[0].rowheader);
                    file.sheets[0].data={};
                }
            }else if(type===1){
                for(let i in file.sheets[0].data){
                    file.sheets[0].data[i].readonly=true;
                }
            }
            that.open(file,type);
        });
        this.ui.addHook('opendatabase',function(o){
            that.hooks.dispatch('opendbtable',o.params);
        });
        this.ui.addHook('newsheet',function(o){
            let file=tools.generateSheets(o.params.tpl_name,o.params.tpl_enname,o.params.tpl_type,o.params.tpl_desc);
            that.open(file,0);
        });
        this.ui.addHook('getsheet',function(o){
            that.sheets.select(o.params.id);
        });
        this.ui.addHook('addsheet',function(o){
            that.sheets.addSheet(o.params.id,o.params.name);
        });
        this.ui.addHook('savesheet',function(o){
            // 保存模板 
            that.savetpl();
        });
        this.ui.addHook('todb',function(o){
            // 创建表 
            that.todb();
        });
        this.ui.addHook('savedata',function(o){
            // 保存数据 
            that.savedata();
        });
        this.ui.addHook('getfile',function(o){
            that.ui.sendfile(that.file,o.params);
        });
        this.ui.addHook('updatefileconf',function(o){
            let data=o.params;
            that.file.name=data.tpl_name;
            that.file.dbname=data.tpl_enname;
            that.file.desc=data.tpl_desc;
            that.file.type=data.tpl_type;
            that.file.dbid=data.tpl_id;
        });

        this.sheets=new Sheets('sheets-container');
        this.sheets.addHook('selectioncell',function(o){
            that.ui.showSelectionCell(o.params);
        });
        this.sheets.addHook('savechart',function(o){
            that.hooks.dispatch('savechart',o.params);
        })
        let taber=new Taber();
    }
    // 编辑模板
    open(data,type){
        this.file=data||tools.generateSheets();
        this.ui.init(this.file.sheets);
        this.sheets.open(this.file.sheets,type);
    }
    // 保存模板
    savetpl(){
        if(this.checktpl()==false){
            alert('该模板基础信息不完整，请先编辑文件属性！');
            return;
        }
        if(this.file.state===2){
            alert('该模板为临时导出数据专用模板，无法进行保存操作！');
            return;
        } 
        this.file.sheets=this.sheets.save();
        this.hooks.dispatch('savetpl',this.file);
    }
    // 保存数据
    savedata(){
        if(this.checktpl()==false){
            alert('该模板基础信息不完整，请先编辑文件属性！');
            return;
        }
        if(this.file.state===2){
            alert('该模板为临时导出数据专用模板，无法进行数据保存操作！');
            return;
        } 
        this.file.sheets=this.sheets.save();
        this.hooks.dispatch('savedata',this.file);
    }
    // 创建表
    todb(){
        if(this.checktpl()==false){
            alert('该模板基础信息不完整，请先编辑文件属性！');
            return;
        }
        if(this.file.state===2){
            alert('该模板为临时导出数据专用模板，无法进行建表操作！');
            return;
        }
        if(!!this.file.dbid){
            alert('该模板已经绑定表信息，无法进行建表操作！');
            return;
        } 
        let err=null;
        let that=this;
        let tablename=that.file.dbname||that.file.name.replace(/ /g,'_');
        let tabledesc=that.file.name+'('+that.file.desc+')';
        let tabletype=that.file.type;
        let table= Pinyin.getFullChars(tablename);
        let db={
                table:table,
                desc:tabledesc,
                charset:'utf-8',
                fields:[]
            };
        this.file.dbname=table;
        let sheet=this.file.sheets[0];
        for(let i in sheet.data){
            let m=sheet.data[i];
            let field=Pinyin.getFullChars(m.value);
            m.dbopts.table=table;
            m.dbopts.field=field;
            db.fields.push({
                name:field,
                type:m.dbopts.type||'varchar',
                def:null,
                note:m.value
            });
        }
        if(db.fields.length<=0){
            err='即将创建的表没有字段信息，请先设置字段信息。';
        }
        if(err===null){
            this.hooks.dispatch('todb',db);
        }else{
            alert(err);
        }
    }
    updatestate(){
        this.file.state=1;
        this.hooks.dispatch('savetpl',this.file);
    }
    checktpl(){
        return !!this.file.name&&!!this.file.type
    }
    parseHeader(key,data){
        if(this.usetype===0) return true;
        let header=true;
        if(tools.isArray(data)){
            let start=key==='col'?data[0].col:data[0].row;
            header=data.map(e=>e.value);
            header=[].concat(this.paddingHeader(start),header);
        }
        return header;
    }
    paddingHeader(start){
        let i=0,parr=[];
        for(;i<start;i++){
            parr.push('');
        }
        return parr
    }
}

export default Marker;